//
//  FPHomeCollectionCell.m
//  FinanceProject
//
//  Created by 陈显摆 on 2025/3/11.
//

#import "FPHomeCollectionCell.h"
#import "FPHomeCollectionViewCell.h"
#import "FPProductModel.h"

@interface FPHomeCollectionCell () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *data;
@property (nonatomic, assign) NSInteger maxItemsInRow;

@end

@implementation FPHomeCollectionCell

- (void)analysisArray:(NSArray *)list {
    
    _data = list;
    
    self.backgroundColor = UIColor.clearColor;
    self.contentView.backgroundColor = UIColor.clearColor;
    
    // 设置最大行数和每行最大列数
    self.maxItemsInRow = 5;  // 每行最大 5 列
    
    NSInteger rowCount = [self calculateRowsForItems:self.data.count];
    
    // 设置布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 4; // 行间距
    layout.minimumInteritemSpacing = 4; // 列间距
    layout.sectionInset = UIEdgeInsetsMake(0, 12, 0, 12); // 左右内边距
//    // 每个 item 的大小
//    layout.itemSize = CGSizeMake(60, 80);  // 设置每个 item 的固定宽度和高度
    
    // 每个 item 的大小
    CGFloat width = (SCREEN_WIDTH - 12 * 2 - 12 * (self.maxItemsInRow - 1)) / self.maxItemsInRow; // 每列的宽度
    layout.itemSize = CGSizeMake(width, 90.f); // 宽高
    
    // 创建 UICollectionView
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(16.f, 0.f, SCREEN_WIDTH - 32.f, 90.f * rowCount) collectionViewLayout:layout];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.scrollEnabled = NO;
    
    UINib *nib = [UINib nibWithNibName:NSStringFromClass(FPHomeCollectionViewCell.class) bundle:nil];
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:@"FPHomeCollectionViewCell"];
    
    self.collectionView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.collectionView];
    
    [self.collectionView reloadData];
    self.collectionView.alwaysBounceHorizontal = NO;
    self.collectionView.alwaysBounceVertical = NO;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.backgroundColor = UIColor.clearColor;
    self.contentView.backgroundColor = UIColor.clearColor;
    
    // 调用接口来判断行数
//    NSInteger rowCount = [self calculateRowsForItems:self.data.count];
//    NSLog(@"Row count: %ld", rowCount); // 输出行数
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma mark - 接口：根据数据计算行数
- (NSInteger)calculateRowsForItems:(NSInteger)itemCount {
    
    // 每行最多 5 个 item，最多 2 行
    NSInteger rows = (itemCount + 5 - 1) / 5; // 向上取整
    return MIN(rows, 2); // 至多显示 2 行
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.data.count; // 返回数据个数
}


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    FPHomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FPHomeCollectionViewCell" forIndexPath:indexPath];
    
    FPProductModel *model = self.data[indexPath.row];
    
//    [cell.mainLab restartLabel];
//    [cell.mainLab shutdownLabel];
    
//    cell.mainLab.labelize = YES;  // 先关闭滚动
    cell.mainLab.text = model.productName;
//    cell.mainLab.labelize = NO;   // 重新开启滚动（会自动重置动画）
    
    [cell.mainImgView sd_setImageWithURL:[NSURL URLWithString:model.productLogo] placeholderImage:IMG(@"icon_home_collection_cell_1")]; // icon_home_placeholder
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
//    FPHomeCollectionModel *model = self.data[indexPath.row];
//    NSLog(@"%@", model.title);
    
    self.transmitBlock(indexPath.row);
}






@end
